﻿using System;
using System.Linq;
using System.Web.Mvc;
using Trirand.Web.Mvc;
using OneDollarStore.Areas.Admin.Models;

namespace OneDollarStore.Areas.Admin.Controllers
{
    [Authorize(Roles="Administrator")]
    public class ManufacturerController : Controller
    {
        StoreModelEntities storeDB = new StoreModelEntities();

        public ViewResult Index()
        {
            ManufacturerJqGridModel gridModel = new ManufacturerJqGridModel();
            // Set grid's parameters
            SetUpGrid(gridModel.ManufacturerGrid);
            return View(gridModel);
        }

        private void SetUpGrid(JQGrid grid)
        {
            grid.ID = "ManufacturerID";
            grid.DataUrl = Url.Action("SearchGridDataRequested");
            grid.EditUrl = Url.Action("EditRows");
            grid.ToolBarSettings.ShowAddButton = true;
            grid.ToolBarSettings.ShowEditButton = true;
            grid.ToolBarSettings.ShowDeleteButton = true;
            grid.ToolBarSettings.ShowSearchButton = true;
            grid.ToolBarSettings.ShowRefreshButton = true;
            grid.AddDialogSettings.CloseAfterAdding = true;
            grid.EditDialogSettings.CloseAfterEditing = true;
        }

        public JsonResult SearchGridDataRequested()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            ManufacturerJqGridModel gridModel = new ManufacturerJqGridModel();
            return gridModel.ManufacturerGrid.DataBind(storeDB.Manufacturers.ToList().AsQueryable());
        }

        public void EditRows()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            ManufacturerJqGridModel gridModel = new ManufacturerJqGridModel();
            var editData = gridModel.ManufacturerGrid.GetEditData();
            if (gridModel.ManufacturerGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                Manufacturer manufacturer = new Manufacturer();
                manufacturer.Name = editData.RowData["Name"];
                manufacturer.Description = editData.RowData["Description"];
                storeDB.AddToManufacturers(manufacturer);
                storeDB.SaveChanges();
            }
            if (gridModel.ManufacturerGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                int manufacturerID = Convert.ToInt32(editData.RowData["ManufacturerID"]);
                Manufacturer manufacturer = (from m in storeDB.Manufacturers
                                             where m.ManufacturerID == manufacturerID
                                             select m).First();
                manufacturer.Name = editData.RowData["Name"];
                manufacturer.Description = editData.RowData["Description"];
                storeDB.SaveChanges();
            }
            if (gridModel.ManufacturerGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                int manufacturerID = Convert.ToInt32(editData.RowData["ManufacturerID"]);
                Manufacturer manufacturer = (from m in storeDB.Manufacturers
                                             where m.ManufacturerID == manufacturerID
                                             select m).First();
                storeDB.DeleteObject(manufacturer);
                storeDB.SaveChanges();
            }
        }
    }
}